package com.buba.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.buba.feign.staffFeign;
import com.buba.mapper.*;
import com.buba.service.SaProvidentService;
import entity.salary.*;
import entity.staff.Accounting;
import org.springframework.stereotype.Service;
import utils.PageResult;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author beisheng
 * @since 2023-03-26
 */
@Service
public class SaProvidentServiceImpl extends ServiceImpl<SaProvidentMapper, SaProvident> implements SaProvidentService {

    @Resource
    private SaProvidentMapper saProvidentMapper;
    @Resource
    private staffFeign staffFeign;
    @Resource
    private SocialMapper socialMapper;
    @Resource
    private AccumulationMapper accumulationMapper;
    @Resource
    private SocialinformationMapper socialinformationMapper;
    @Resource
    private SaAccountMapper saAccountMapper;

    /* *
     * @Author: beisheng
     * @Descriprion: 新建社保公积金台账,并添加关联人员
     * @Date: 2023/3/26 18:55
     * @Return: common.R
     * @Param: [list]
     */

    @Override
    public void addNewProvicent(Map<String,Object> map) {
        List<Integer> list = (List<Integer>) map.get("list");
        Date accountTime = (Date) map.get("accountTime");
        //新建台账
        SaAccount saAccount= new SaAccount();
        saAccount.setCreateTime(new Date());
        saAccount.setUpdateTime(new Date());
        saAccount.setDeleteFlag(0);
        saAccount.setAcountTime(accountTime);
        saAccountMapper.insert(saAccount);
        List<String> stringList = new ArrayList<>();
        for (Integer integer : list) {
            stringList.add(String.valueOf(integer));
        }
        //1.根据ID查询员工信息
        List<Accounting> accountings = staffFeign.selstaffByIds(stringList);
        //2.根据查询出的用户关联社保公积金ID查询社保和公积金方案
        for (Accounting accounting : accountings) {
            SaProvident saProvident = new SaProvident();
            Social social = socialMapper.selectOne(new QueryWrapper<Social>().eq("delete_flag", 0).eq("sa_id", accounting.getSecurityId()));
            Accumulation accumulation = accumulationMapper.selectOne(new QueryWrapper<Accumulation>().eq("delete_flag", 0).eq("sa_id", accounting.getAccumulationId()));
            //查询社保方案公司缴纳数
            List<Socialinformation> socialGongsiList = socialinformationMapper.selectList(new QueryWrapper<Socialinformation>().eq("delele_flag", 0).eq("social_security",1).eq("social_id", social.getSaId()).eq("payl_name", "公司"));
            //查询社保方案个人缴纳数
            List<Socialinformation> socialGerenList = socialinformationMapper.selectList(new QueryWrapper<Socialinformation>().eq("delele_flag", 0).eq("social_security",1).eq("social_id", social.getSaId()).eq("payl_name", "个人"));
            //查询公积金方案个人缴纳数
            List<Socialinformation> accumGongsiList = socialinformationMapper.selectList(new QueryWrapper<Socialinformation>().eq("delele_flag", 0).eq("social_security",2).eq("social_id", accumulation.getSaId()).eq("payl_name", "公司"));
            //查询公积金方案个人缴纳数
            List<Socialinformation> accumGerenList = socialinformationMapper.selectList(new QueryWrapper<Socialinformation>().eq("delele_flag", 0).eq("social_security",2).eq("social_id", accumulation.getSaId()).eq("payl_name", "个人"));

                //将公司缴纳社保数值存入对象
                for (Socialinformation socialGongsi : socialGongsiList) {
                    saProvident.setSocialSecurity(socialGongsi.getPayCardinal());
                    Double socialCardinal = socialGongsi.getPayCardinal();
                        if(!saProvident.getEmploymentMy().equals(null)){
                            //判断是否为养老保险
                            if (socialGongsi.getSociaName() == 1) {
                                Double aDouble = Double.valueOf(socialGongsi.getPayScale().substring(0, socialGongsi.getPayScale().length() - 1));
                                saProvident.setEndowmentMy(socialCardinal * aDouble);
                            } else {
                                saProvident.setEndowmentMy(0.0);
                            }
                        }
                        if(!saProvident.getUnemploymentMy().equals(null)) {
                            //判断是否为失业保险
                            if (socialGongsi.getSociaName() == 3) {
                                Double aDouble = Double.valueOf(socialGongsi.getPayScale().substring(0, socialGongsi.getPayScale().length() - 1));
                                saProvident.setUnemploymentMy(socialCardinal * aDouble);
                            } else {
                                saProvident.setUnemploymentMy(0.0);
                            }
                        }
                        if(!saProvident.getEmploymentMy().equals(null)) {
                            //判断是否为工伤保险
                            if (socialGongsi.getSociaName() == 4) {
                                Double aDouble = Double.valueOf(socialGongsi.getPayScale().substring(0, socialGongsi.getPayScale().length() - 1));
                                saProvident.setEmploymentMy(socialCardinal * aDouble);
                            } else {
                                saProvident.setEmploymentMy(0.0);
                            }
                        }
                        if(!saProvident.getMaternityMy().equals(null)) {
                            //判断是否为生育保险
                            if (socialGongsi.getSociaName() == 5) {
                                Double aDouble = Double.valueOf(socialGongsi.getPayScale().substring(0, socialGongsi.getPayScale().length() - 1));
                                saProvident.setMaternityMy(socialCardinal * aDouble);
                            } else {
                                saProvident.setMaternityMy(0.0);
                            }
                        }
                        if(!saProvident.getMedicalMy().equals(null)) {
                            //判断是否为医疗保险
                            if (socialGongsi.getSociaName() == 6) {
                                Double aDouble = Double.valueOf(socialGongsi.getPayScale().substring(0, socialGongsi.getPayScale().length() - 1));
                                saProvident.setMedicalMy(socialCardinal * aDouble);
                            } else {
                                saProvident.setMedicalMy(0.0);
                            }
                    }
                }

                //将个人缴纳社保数值存入对象
                for (Socialinformation socialGeren : socialGerenList) {
                    saProvident.setSocialSecurity(socialGeren.getPayCardinal());
                    Double socialCardinal = socialGeren.getPayCardinal();
                        if(!saProvident.getEmploymentThem().equals(null)){
                            //判断是否为养老保险
                            if (socialGeren.getSociaName() == 1) {
                                Double aDouble = Double.valueOf(socialGeren.getPayScale().substring(0, socialGeren.getPayScale().length() - 1));
                                saProvident.setEndowmentThem(socialCardinal * aDouble);
                            } else {
                                saProvident.setEndowmentThem(0.0);
                            }
                        }
                        if(!saProvident.getUnemploymentThem().equals(null)) {
                            //判断是否为失业保险
                            if (socialGeren.getSociaName() == 3) {
                                Double aDouble = Double.valueOf(socialGeren.getPayScale().substring(0, socialGeren.getPayScale().length() - 1));
                                saProvident.setUnemploymentThem(socialCardinal * aDouble);
                            } else {
                                saProvident.setUnemploymentThem(0.0);
                            }
                        }
                        if(!saProvident.getEmploymentThem().equals(null)) {
                            //判断是否为工伤保险
                            if (socialGeren.getSociaName() == 4) {
                                Double aDouble = Double.valueOf(socialGeren.getPayScale().substring(0, socialGeren.getPayScale().length() - 1));
                                saProvident.setEmploymentThem(socialCardinal * aDouble);
                            } else {
                                saProvident.setEmploymentThem(0.0);
                            }
                        }
                        if(!saProvident.getMaternityMy().equals(null)) {
                            //判断是否为生育保险
                            if (socialGeren.getSociaName() == 5) {
                                Double aDouble = Double.valueOf(socialGeren.getPayScale().substring(0, socialGeren.getPayScale().length() - 1));
                                saProvident.setMaternityThem(socialCardinal * aDouble);
                            } else {
                                saProvident.setMaternityThem(0.0);
                            }
                        }
                        if(!saProvident.getMedicalThem().equals(null)) {
                            //判断是否为医疗保险
                            if (socialGeren.getSociaName() == 6) {
                                Double aDouble = Double.valueOf(socialGeren.getPayScale().substring(0, socialGeren.getPayScale().length() - 1));
                                saProvident.setMedicalThem(socialCardinal * aDouble);
                            } else {
                                saProvident.setMedicalThem(0.0);
                            }
                    }
                }

                //将公司缴纳公积金数值存入对象
                for (Socialinformation accumGongsi : accumGongsiList) {
                    saProvident.setSocialSecurity(accumGongsi.getPayCardinal());
                    Double socialCardinal = accumGongsi.getPayCardinal();
                            //判断是否为养老保险
                            if (accumGongsi.getSociaName() == 2) {
                                Double aDouble = Double.valueOf(accumGongsi.getPayScale().substring(0, accumGongsi.getPayScale().length() - 1));
                                saProvident.setAccumMy(socialCardinal * aDouble);
                            } else {
                                saProvident.setAccumMy(0.0);
                            }
                }

                //将个人缴纳公积金数值存入对象
                for (Socialinformation accumGeren : accumGerenList) {
                    saProvident.setAccumSecurity(accumGeren.getPayCardinal());
                    Double socialCardinal = accumGeren.getPayCardinal();
                    //判断是否为养老保险
                    if (accumGeren.getSociaName() == 2) {
                        Double aDouble = Double.valueOf(accumGeren.getPayScale().substring(0, accumGeren.getPayScale().length() - 1));
                        saProvident.setAccumThem(socialCardinal * aDouble);
                    } else {
                        saProvident.setAccumThem(0.0);
                    }
                }

            //个人社保合计
            saProvident.setSocialMyamount(saProvident.getEndowmentThem()+saProvident.getUnemploymentThem()+saProvident.getEmploymentThem()+saProvident.getMaternityThem());
            //公司社保合计
            saProvident.setSocialThemamount(saProvident.getEndowmentMy()+saProvident.getUnemploymentMy()+saProvident.getEmploymentMy()+saProvident.getMaternityMy());
            //个人公积金合计
            saProvident.setAccumMyamount(saProvident.getAccumThem());
            //公司公积金合计
            saProvident.setAccumThemamount(saProvident.getAccumMy());
            //社保合计
            saProvident.setSocialAmount(saProvident.getSocialMyamount()+saProvident.getSocialThemamount());
            //公积金合计
            saProvident.setAccumAmount(saProvident.getAccumMyamount()+saProvident.getAccumThemamount());
            //缴纳合计
            saProvident.setTotalAmount(saProvident.getSocialAmount()+saProvident.getAccumAmount());
            //创建时间
            saProvident.setCreateTime(new Date());
            //修改时间
            saProvident.setUpdateTime(new Date());
            //删除标识
            saProvident.setDeleteFlag(0);
            //关联台账ID
            saProvident.setAccountId(saAccount.getSaId());
            //3.根据查询出的社保公积金方案进行添加到台账中
            saProvidentMapper.insert(saProvident);
        }
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 分页条件查询台账列表
     * @Date: 2023/3/28 8:30
     * @Return: common.R
     * @Param: [date]
     */

    @Override
    public PageResult selAllSaProvident(Map<String, Object> map) {
        Integer page = (Integer) map.get("page");
        Integer pageNum = (Integer) map.get("pageNum");
        String mounth = (String) map.get("mounth");
        Page<SaAccount> saProvidentPage = new Page<>(page,pageNum);
        QueryWrapper<SaAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("delete_flag",0);
        if(!mounth.equals(null)){
            queryWrapper.like("acount_time",mounth);
        }
        Page<SaAccount> saAccountPage = saAccountMapper.selectPage(saProvidentPage, queryWrapper);
        List<SaAccount> saAccounts = saAccountMapper.selectList(queryWrapper);
        for (SaAccount record : saAccountPage.getRecords()) {
            List<SaProvident> saProvidents = saProvidentMapper.selectList(new QueryWrapper<SaProvident>().eq("delete_flag", 0).eq("account_id", record.getSaId()));
            for (SaProvident saProvident : saProvidents) {
                record.setSocialCount(record.getSocialCount() + saProvident.getSocialAmount());
                record.setAccumCount(record.getAccumCount() + saProvident.getAccumAmount());
            }
        }
        return new PageResult(page,pageNum,saAccounts.size(),saAccountPage.getRecords());
    }

    /* *
     * @Author: beisheng
     * @Descriprion: 根据台账ID分页查询台账明细
     * @Date: 2023/3/31 10:11
     * @Return: common.R
     * @Param: [proId, pageSize, pageNum]
     */

    @Override
    public PageResult selDetailed(Integer proId, Integer pageSize, Integer pageNum) {
        Page<SaProvident> saProvidentPage = new Page<>(pageSize,pageNum);
        Page<SaProvident> saProvidentPages = saProvidentMapper.selectPage(saProvidentPage, new QueryWrapper<SaProvident>().eq("delete_flag", 0).eq("account_id",proId));
        List<SaProvident> saProvidents = saProvidentMapper.selectList(new QueryWrapper<SaProvident>().eq("delete_flag", 0).eq("account_id",proId));
        return new PageResult(pageSize,pageNum,saProvidents.size(),saProvidentPage.getRecords());
    }

}
